#! /usr/bin/python3
# coding=utf-8
import os
import argparse

from parse.ipcp_parser import APVPParser
from parse.sdb_parser import SDBParser
from parse.some_ip_parse import SomeIPParser
from generator.types_hal.types_hal_parse import TypesHalPaser
from utils.util import removeDir


def getArgs():
    parser = argparse.ArgumentParser(
        description="脚本生成工具,用来创建网络处理包,以及types.hal等内容"
    )
    subparser = parser.add_subparsers()
    transferParser = subparser.add_parser(
        "genTransfer", help="用来创建网络处理包,以及types.hal等内容")
    transferParser.add_argument(
        '-n', '--net', help="生成网络包处理", action="store_true")
    transferParser.add_argument(
        '-p', '--property', help="types.hal file construction", action="store_true")

    transferParser.add_argument(
        '-l', '--lib',  help="需要解析的内容", action="append", choices=["someip", "apvp"], type=str)

    transferParser.add_argument(
        '--someip_target',  help="someip使用,用来确定someip的发送方和接收方", choices=['slave', 'master'], type=str)
    transferParser.set_defaults(transferFunc=taskTransfer)

    upgradeSDB_Parser = subparser.add_parser(
        "upgradeSDB", help="更新SDB")
    upgradeSDB_Parser.add_argument(
        '-l', '--legacy', help="该选项用来转换历史遗留的旧TXRX的xml文件(通常不需要)", action="store_true")
    upgradeSDB_Parser.set_defaults(upgradeSBDFunc=upgradeSDB)
    return (parser, transferParser, upgradeSDB_Parser)


def taskTransfer(args, parser):
    someIPParser = None
    apvpParser = None
    if args["net"] or args["property"]:
        typesHalPaser = TypesHalPaser(configDir)
        typesHalPaser.parse()
        target = args['someip_target']
        libs = args['lib']
        if args["property"]:
            apvpParser = APVPParser(configDir)
            apvpParser.parse(typesHalPaser.directTransmitList)
            if target is None:
                parser.error("缺少--someip_target参数")
            someIPParser = SomeIPParser(configDir, target)
            someIPParser.parse()
        else:
            if "apvp" in libs:
                apvpParser = APVPParser(configDir)
                apvpParser.parse(typesHalPaser.directTransmitList)
            if "someip" in libs:
                if target is None:
                    parser.error("缺少--someip_target参数")
                someIPParser = SomeIPParser(configDir, target)
                someIPParser.parse()
        if args["net"]:
            if "apvp" in libs:
                apvpParser.genertor_files()
            if "someip" in libs:
                someIPParser.genertor_files()
        if args["property"]:
            # 先清理透传信号实现目录,因为每次脚本运行都会重新生成新的透传实现
            removeDir("../vhal_platform/directTransmit")
            if apvpParser is not None:
                apvpTypesHal = apvpParser.genertorDirectTransmitImpl()
                typesHalPaser.typesHal.extend(apvpTypesHal)

            if someIPParser is not None:
                someipTypesHal = someIPParser.toDirectTransmitImpl()
                typesHalPaser.typesHal.extend(someipTypesHal)
            # 创建types.hal文件
            typesHalPaser.genertor_file()
    else:
        parser.print_help()


def upgradeSDB(legacy):
    sdbParser = SDBParser()
    sdbParser.upgradeSDB(legacy)


if __name__ == "__main__":
    configDir = os.path.abspath(os.path.join(os.getcwd(), "config"))
    (parser, transfer, upgrade) = getArgs()
    parse_args = parser.parse_args()
    args = vars(parse_args)
    if len(args.items()) == 0:
        parser.print_help()
        parser.exit(1)
    if hasattr(parse_args, "transferFunc"):
        parse_args.transferFunc(args, transfer)
    elif hasattr(parse_args, "upgradeSBDFunc"):
        parse_args.upgradeSBDFunc(args['legacy'])
